public class num234 {
    public boolean isPalindrome(ListNode head) {
        ListNode middle = middleNode(head);
        ListNode head2 = reverseList(middle);
        while (head2 != null){
            if(head.val != head2.val){
                return false;
            }
            head = head.next;
            head2 = head2.next;
        }
        return true;
    }
    public ListNode middleNode(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode fast = head;
        ListNode snow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            snow = snow.next;
        }
        return snow;
    }
    public ListNode reverseList(ListNode head) {
        ListNode dummyhead = new ListNode();
        ListNode prev = head;
        while (prev != null){
            ListNode node = new ListNode(prev.val);
            node.next = dummyhead.next;
            dummyhead.next = node;
            prev = prev.next;
        }
        return dummyhead.next;
    }
}
